package view;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.function.Consumer;

public class Menu {
    ArrayList<Fruit> fruits = new ArrayList<>();
    {
        try {

            loadData();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    Scanner scanner = new Scanner(System.in);
    public  Integer showMenu() {

        System.out.println("=====================欢迎使用水果库存系统=====================");
        System.out.println("1.显示库存列表");
        System.out.println("2.添加库存记录");
        System.out.println("3.查看特定库存");
        System.out.println("4.水果下架");
        System.out.println("5.退出");
        System.out.println("============================================================");

        System.out.println("请选择数字");

        return scanner.nextInt();
    }


    public void loadData() throws IOException {
        InputStream resourceAsStream = Menu.class.getClassLoader().getResourceAsStream("a.da");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream));
        String b;
        while ((b = bufferedReader.readLine()) != null) {
            String[] split = b.split("  ");
            String name = split[0];
            int price = Integer.parseInt(split[1]);
            int inventory = Integer.parseInt(split[2]);
            String remark = split[3];
            fruits.add(new Fruit(name, price, inventory, remark));

        }
        bufferedReader.close();

    }

    public void showdata() {
        System.out.println("------------------------------------------------------------");
        System.out.println("名称\t\t单价\t\t库存\t\t备注");
        if (fruits == null || fruits.size() <= 0) {
            System.out.println("对不起库存为空");
        } else {
          fruits.forEach(new Consumer<Fruit>() {
              @Override
              public void accept(Fruit fruit) {
                  System.out.println(fruit);
              }
          });
        }
    }

    public void add() {

        System.out.println("请输入要添加的品种名称");
        String s = scanner.next();
        for (Fruit fruit : fruits) {
            if (s.equals(fruit.getName())) {
                System.out.println("请输入要添加的库存数量");
                int i = scanner.nextInt();
                fruit.setInventory(fruit.getInventory() + i);
                System.out.println("添加成功");
            } else {
                System.out.println("请输入单价");
                int price = scanner.nextInt();

                System.out.println("请输入库存");
                int count = scanner.nextInt();
                System.out.println("请输入备注");
                String remark = scanner.next();

                Fruit fruit1 = new Fruit(s, price, count, remark);
                fruits.add(fruit1);
                System.out.println("添加成功");
            }
            return;

        }

    }

    public void search() {

        System.out.println("请输入要查看的品种名称");
        String s = scanner.next();
        for (Fruit fruit : fruits) {
            if (s.equals(fruit.getName())) {
                System.out.println(s + "库存为" + fruit.getInventory());
            } else {
                System.out.println("对不起没有该品种");
            }
            return;
    }
}

    public void drop() {

        System.out.println("请输入要删除的品种名称");
        String s = scanner.next();
        for (Fruit fruit : fruits) {
            if (s.equals(fruit.getName())) {
                boolean remove = fruits.remove(fruit);
                System.out.println("删除成功");
            } else {
                System.out.println("对不起没有该品种");
            }
            return;
    }
}

    public void exit() throws IOException {
        System.out.println("拜拜");
        savedata();
        System.exit(3);

    }

    private void savedata() throws IOException {
        File file = new File("..\\pro_javaweb\\pro02-fruit0.1\\src\\a.da");
        FileWriter fileWriter = new FileWriter(file);
        PrintWriter printWriter = new PrintWriter(fileWriter);
        fruits.forEach(fruit -> printWriter.println((fruit.getName() + "  " + fruit.getPrice() + "  " + fruit.getInventory() + "  " + fruit.getRemark())));
        printWriter.flush();

    }
}
